package com.example.demo01.controller;


import com.example.demo01.Entity.Client;
import com.example.demo01.Service.ClientService;
import com.example.demo01.Service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/api/client")  // 确保路径清晰，避免冲突
public class ClientController {

    @Autowired
    private ClientService clientService;
    @Autowired
    private TagService tagService;


    @RequestMapping
    public String getTagsPage(Model model) {
        List<Client> clients = clientService.getAllClients();  // 获取所有客户
        model.addAttribute("clients", clients);
        System.out.println("客户列表：" + clients );  // 打印客户列表
        if (!clients.isEmpty()) {
            System.out.println("第一个客户的名字：" + clients.get(0).getName());  // 打印第一个客户的名字
        }
        return "client";  // 返回 client.html 页面
    }

    // 获取客户及其所有标签
    @RequestMapping("/client/{clientId}")
    public String getClient(@PathVariable Long clientId, Model model) {
        Client client = clientService.getClientWithTags(clientId);
        model.addAttribute("client", client);  // 将客户及其标签传递到视图
        return "clientDetail";  // 返回客户详情视图
    }

    // 创建客户
    @PostMapping("/create")
    public String createClient(@RequestParam String name) {
        Client client = new Client(name);  // 创建客户
        clientService.saveClient(client);  // 保存客户
        return "redirect:/api/client";  // 重定向到客户页面
    }


    // 显示编辑客户的表单
    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable Long id, Model model) {
        Optional<Client> client = clientService.getClientById(id);
        if (client.isPresent()) {
            System.out.println("编辑客户的名字：" + client.get().getName());  // 打印编辑的客户名称
            model.addAttribute("client", client.get());  // 将客户对象传递给模板
            return "editClient";  // 返回 editClient.html 页面
        }
        return "redirect:/api/client";  // 客户不存在则重定向到客户列表
    }


    // 更新客户信息
    @PostMapping("/edit/{id}")
    public String updateClient(@PathVariable Long id, @RequestParam String name) {
        Optional<Client> existingClient = clientService.getClientById(id);
        if (existingClient.isPresent()) {
            Client client = existingClient.get();
            client.setName(name);  // 更新客户的名称
            clientService.saveClient(client);  // 保存更新后的客户
        }
        return "redirect:/api/client";  // 重定向到客户列表
    }



    // 删除客户
    @GetMapping("/delete/{id}")
    public String deleteClient(@PathVariable Long id) {
        clientService.deleteClient(id);  // 删除客户
        return "redirect:/api/client";  // 重定向到客户页面
    }


@PostMapping("/{id}/add-label")
public String addLabelToClient(@PathVariable Long id, @RequestParam String labelNames) {
    System.out.println("Received labelNames: " + labelNames);  // 打印接收到的标签数据
    try {
        String[] labels = labelNames.split(",");
        for (String label : labels) {
            tagService.addTagToClient(id, label.trim());  // 添加标签到客户
        }
        return "redirect:/api/client";  // 重定向到客户页面
    } catch (Exception e) {
        e.printStackTrace();
        return "redirect:/api/client";  // 出现错误时，重定向到客户页面
    }
}



}
